package com.hyt.it.ogt.kq.main.controller.gov;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.util.StringUtils;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.service.gov.model.entity.TaskArea;
import com.hyt.it.ogt.kq.service.gov.model.entity.TaskPlace;
import com.hyt.it.ogt.kq.service.gov.model.entity.Time;
import com.hyt.it.ogt.kq.service.gov.model.param.ExamMonitorParam;
import com.hyt.it.ogt.kq.service.gov.model.param.GetRoomTreeParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.ExamMonitorVO;
import com.hyt.it.ogt.kq.common.gov.utils.tree.Tree;
import com.hyt.it.ogt.kq.common.gov.secmodule.SecModuleUtil;
import com.hyt.it.ogt.kq.service.gov.service.IExamMonitorService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskAreaService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskPlaceService;
import com.hyt.it.ogt.kq.service.gov.service.ITimeService;
import com.hyt.it.ogt.kq.common.gov.utils.tree.TreeUtil;
import com.hyt.model.PageParam;
import com.hyt.obt.common.CSecModule;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 考试过程监控
 * </p>
 *
 * @author liuq
 * @since 2021-04-22
 */
@RestController
@Api(tags = "108.考试过程监控接口", value = "考生接口")
@ApiSort(value = 108)
@RequestMapping("/kw/gov/examMonitor")
@Slf4j
public class ExamMonitorController {

	@Autowired
	private ITimeService iTimeService;
	
	@Autowired
	private ITaskAreaService iTaskAreaService;
	
	@Autowired
	private ITaskPlaceService iTaskPlaceService;
	
	@Autowired
	private IExamMonitorService iExamMonitorService;
	
	@ApiOperation(
		value = "8.1  查询批次树",
		notes = "30000：成功"
	)
	@ApiOperationSort(value = 1)
	@ApiImplicitParams({
		@ApiImplicitParam(
			name = "taskId", value = "考试任务id", dataType = "string", 
			paramType = "query", required = true
		)
	})
	@RequestMapping(value = "/get/time/tree", method = RequestMethod.GET)
	public ApiResponse<List<Tree>> getTimeTree(String taskId) {
		QueryWrapper<Time> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
			.select(Time::getId, Time::getName)
			.eq(Time::getTaskId, taskId)
			.eq(Time::getDelFlag, false)
			.orderByAsc(Time::getCreateDate);
		List<Time> timeList = iTimeService.list(queryWrapper);
		List<Tree> treeList = timeList.stream().map(time -> {
				Tree tree = new Tree();
				tree.setValue(time.getId())
						.setLabel(time.getName());

				return tree;
			}).collect(Collectors.toList());
		return ApiResponse.<List<Tree>>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.data(treeList)
				.build();
	}
	
	@ApiOperation(
		value = "8.2  查询区域树",
		notes = "30000：成功"
	)
	@ApiOperationSort(value = 2)
	@ApiImplicitParams({
		@ApiImplicitParam(
			name = "taskId", value = "考试任务id", dataType = "string", 
			paramType = "query", required = true
		)
	})
	@RequestMapping(value = "/get/area/tree", method = RequestMethod.GET)
	public ApiResponse<List<Tree>> getAreaTree(String taskId) {
		QueryWrapper<TaskArea> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
			.select(TaskArea::getAreaId, TaskArea::getParentId, TaskArea::getName, TaskArea::getType)
			.eq(TaskArea::getTaskId, taskId)
			.eq(TaskArea::getDelFlag, false)
			.orderByAsc(TaskArea::getCreateDate);
		List<TaskArea> areaList = iTaskAreaService.list(queryWrapper);
		List<Tree> treeList = areaList.stream().map(area -> {
				Tree tree = new Tree();
				tree.setValue(area.getAreaId())
						.setLabel(area.getName())
						.setObj(area.getType())
						.setPid(area.getParentId());

				/*tree.setId(area.getAreaId())
					.setPid(area.getParentId())
					.setText(area.getName())
					.setObj(area.getType());*/
				return tree;
			}).collect(Collectors.toList());
		treeList = TreeUtil.treeToMultilevel(treeList);
		return ApiResponse.<List<Tree>>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.data(treeList)
				.build();
	}
	
	@ApiOperation(
		value = "8.3  查询考点树",
		notes = "30000：成功"
	)
	@ApiOperationSort(value = 3)
	@ApiImplicitParams({
		@ApiImplicitParam(
			name = "taskId", value = "考试任务id", dataType = "string", 
			paramType = "query", required = true
		),
		@ApiImplicitParam(
			name = "areaId", value = "区域id", dataType = "string", 
			paramType = "query", required = false
		),
		@ApiImplicitParam(
			name = "areaType", value = "区域类型（区域树的obj）", dataType = "string", 
			paramType = "query", required = false
		)
	})
	@RequestMapping(value = "/get/place/tree", method = RequestMethod.GET)
	public ApiResponse<List<Tree>> getPlaceTree(String taskId, String areaId, String areaType) {
		QueryWrapper<TaskPlace> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
			.select(TaskPlace::getId, TaskPlace::getName)
			.eq(TaskPlace::getTaskId, taskId)
			.eq(TaskPlace::getDelFlag, false)
			.orderByAsc(TaskPlace::getCreateDate);
		if (StringUtils.isNotEmpty(areaType) && StringUtils.isNotEmpty(areaId)) {
			switch (areaType) {
				case "2":
					queryWrapper.lambda().eq(TaskPlace::getProvince, areaId);
					break;
				case "3":
					queryWrapper.lambda().eq(TaskPlace::getCity, areaId);
					break;
				case "4":
					queryWrapper.lambda().eq(TaskPlace::getDistrict, areaId);
					break;
				default:
					break;
			}
		}
		List<TaskPlace> placeList = iTaskPlaceService.list(queryWrapper);
		List<Tree> treeList = placeList.stream().map(place -> {
				Tree tree = new Tree();
				tree.setValue(place.getId())
						.setLabel(place.getName());

				/*tree.setId(place.getId())
					.setText(place.getName());*/
				return tree;
			}).collect(Collectors.toList());
		return ApiResponse.<List<Tree>>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.data(treeList)
				.build();
	}
	
	@ApiOperation(
		value = "8.4  查询考场树",
		notes = "30000：成功"
	)
	@ApiOperationSort(value = 4)
	@RequestMapping(value = "/get/room/tree", method = RequestMethod.GET)
	public ApiResponse<List<Tree>> getRoomTree(GetRoomTreeParam param) {
		List<Tree> treeList = iExamMonitorService.getRoomTree(param);
		return ApiResponse.<List<Tree>>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.data(treeList)
				.build();
	}
	
	@ApiOperation(
		value = "8.5 查询考试过程监控分页数据",
		notes = "30000：成功"
	)
	@ApiOperationSort(value = 5)
	@RequestMapping(value = "/get/page", method = RequestMethod.GET)
	public ApiResponse<Page<ExamMonitorVO>> getPage(PageParam<ExamMonitorVO> pageParam, ExamMonitorParam param) {
		log.debug("# 查询考试过程监控分页数据参数 ： {}", param);
		Page<ExamMonitorVO> page = iExamMonitorService.page(pageParam, param);
		return ApiResponse.<Page<ExamMonitorVO>>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.data(page)
				.build();
	}
	
	@ApiOperation(
		value = "8.6 测试加密字符串"
	)
	@ApiOperationSort(value = 6)
	@RequestMapping(value = "/encryp", method = RequestMethod.GET)
	public String encrypString(String text) {
		return SecModuleUtil.encrypString(text);
	}
	
	@ApiOperation(
		value = "8.7 测试解密字符串"
	)
	@ApiOperationSort(value = 7)
	@RequestMapping(value = "/decryp", method = RequestMethod.GET)
	public String decrypString(String text) {
		return SecModuleUtil.decrypString(text);
	}

}
